﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Builder;
using Autofac.Integration.Web;
using Autofac.Integration.Web.Mvc;
using MooNet.Web.Api.Lib;
using MooNet.Web.Api.Lib.Interfaces;

namespace MooNet.Api.ExampleConsumer
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static IContainer m_container;
        private static IContainerProvider m_containerProvider;

        private void RegisterDependencies()
        {
            // when the application starts, we need to build the container and register all
            // the dependencies that we want it to manage for us
            var builder = new ContainerBuilder();

            RegisterGigJunkieApiDependencies(builder);

            // and then we'll use the AutofacControllerModule to register all the controllers it finds in the given assembly
            builder.RegisterModule(new AutofacControllerModule(Assembly.GetExecutingAssembly()));

            // finally we build the container...
            m_container = builder.Build();
            m_containerProvider = new ContainerProvider(m_container);

            // ...and notify ASP.NET MVC that it should use Autofacs own controller factory to instantiate controllers,
            // so that Autofac can resolve any dependencies the controllers need
            ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(m_containerProvider));
        }

        private void RegisterGigJunkieApiDependencies(ContainerBuilder builder)
        {
            string apiBaseUrl = ConfigurationManager.AppSettings["ApiBaseUrl"];
            if (string.IsNullOrEmpty(apiBaseUrl))
            {
                throw new NotImplementedException(
                    "Please paste the base url of the version of the API you are using into the appSettings section of your Web.config");
            }

            string consumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
            if (string.IsNullOrEmpty(consumerKey))
            {
                throw new NotImplementedException("Please paste your consumer key into the appSettings section of your Web.config");
            }

            builder.Register<GigJunkieApiService>().As<IGigJunkieApiService>().SingletonScoped();
            builder.Register<GigJunkieApiWrapper>().As<IGigJunkieApiWrapper>().SingletonScoped();

            GigJunkieApiSettings settings = new GigJunkieApiSettings(apiBaseUrl, consumerKey);
            builder.Register<IGigJunkieApiSettings>(x => settings);
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            RegisterDependencies();
        }

    }
}